﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LPY.LeHealth.DAL;
using LPY.LeHealth.Model;
using AutoMapper;
using AutoMapper.Execution;
using AutoMapper.Configuration;
using AutoMapper.Mappers;
using LPY.LeHealth.DTO;


namespace LPY.LeHealth.Service
{
    public class OrderService
    {
        private OrderDAL orderDal;
        private OrderDetailDAL orderDetailDal;
        private CartDal carDal;
        private PhysicalPeopleDal physicalPeopleDal;

        public OrderService()
        {
            orderDal = new OrderDAL();
            orderDetailDal = new OrderDetailDAL();
            carDal = new CartDal();
            physicalPeopleDal = new PhysicalPeopleDal();
        }

        /// <summary>
        /// 显示医院列表
        /// </summary>
        /// <returns></returns>
        public List<Order> ShowOrderHospital()
        {
            var order = orderDal.ShowOrderList();
            var rtv = Mapper.Map<List<Order>, List<Order>>(order);
            return rtv;
        }
		/// <summary>
		/// 根据用户ID判断该用户是否用购买记录
		/// </summary>
		/// <param name="UserId">用户编号</param>
		/// <returns>返回购买是否有购买记录</returns>
       
		public int OrderByUser(int UserId)
		{
			var order = orderDal.GetOrder(UserId);
			var rtv = Mapper.Map<List<Order>, List<Order>>(order).Count();
			return rtv;
		}

        /// <summary>
        /// 创建订单
        /// </summary>
        /// <param name="order"></param>
        /// <returns></returns>
        public long CreateOrder(Order order)
        {
            //计算总金额
            //var comboList = carDal.SelectByUserId(order.UserId);
            //Decimal total = 0;
            //for (int i = 0; i < comboList.Count; i++)
            //{
            //    total += comboList[i].Combo.Price*comboList[i].Count;
            //}
            //order.SumPrice = total;
            order.Ordercode = Guid.NewGuid().ToString();//生成订单编号
            //order.CreateDate = DateTime.Now;

            long id=0;
            orderDal.Insert(order,out id);

            //订单创建成功移除购物车数据
            //if (id > 0)
            //{
            //    //添加订单商品明细
            //    List<OrderDetail> ods = new List<OrderDetail>();
            //    OrderDetail od;
            //    for (int i = 0; i < comboList.Count; i++)
            //    {
            //        od = new OrderDetail();
            //        od.OrderId = id;
            //        od.Count = comboList[i].Count;
            //        od.ComboId = comboList[i].ComboId;
            //        od.UserId = order.UserId;
            //        ods.Add(od);
            //    }
            //    orderDetailDal.InsertAll(ods);

            //    //清空购物车
            //    carDal.Clear(order.UserId);
            //}
            return id;
        }

        public Order GetById(int oid)
        {
            return orderDal.SelectById(oid);
        }

        /// <summary>
        /// 添加预约信息
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public bool AddAppointmentInfo(List<PhysicalPeople> infos)
        {
             physicalPeopleDal.Insert(infos);
             return true;
        }

        /// <summary>
        /// 更新订单状态
        /// </summary>
        /// <param name="orderId">订单Id</param>
        /// <param name="state">订单状态</param>
        /// <returns></returns>
        public bool UpdateOrderState(int orderId,OrderState state)
        {
            return orderDal.UpdateState(orderId, (int)state);
        }

        /// <summary>
        /// 获取订单列表
        /// </summary>
        /// <param name="uid">用户Id</param>
        /// <param name="sate">状态 -1：表示所有订单</param>
        /// <returns></returns>
        public List<Order> GetOrderList(int uid,int sate)
        {
            List<Order> rtv = new List<Order>();
            if (sate<0)
            {
                rtv = orderDal.GetList(uid);
            }
            else
            {
                rtv= orderDal.GetList(uid, sate);
            }
            return rtv;
        }




        /// <summary>
        /// 根据用户名与该用户的订单Id获取该订单信息
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<Order> GetIndentList(int uid, int id)
        {
            return orderDal.GetIndentList(uid, id);
        }


        /// <summary>
        /// 获取订单详情
        /// </summary>
        /// <param name="orderId">订单Id</param>
        /// <returns></returns>
        public Order GetOrderDetails(int orderId)
        {
            return orderDal.GetById(orderId);
        }

        /// <summary>
        /// 获取订单类型总数
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public Dictionary<int,int> GetOrderStateCount(int uid)
        {
            var dic= orderDal.GetOrderStateCount(uid);
            if(dic.Count==0)
            {
                dic[0] = 0;
                dic[1] = 0;
                dic[2] = 0;
            }
            else if(dic.Count==2)
            {
                dic[2] = 0;
            }
            else if(dic.Count==1)
            {
                dic[1] = 0;
                dic[2] = 0;
            }
            return dic;
        }

        /// <summary>
        /// 获取订单数
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public int GetOrderCount(int uid)
        {
            return orderDal.GetOrderCount(uid);
        }
        /// <summary>
        /// 根据订单Id获取预约人信息
        /// </summary>
        /// <param name="oid"></param>
        /// <returns></returns>
        public List<PhysicalPeople> GetPhysicalPeopleByOrderId(int oid)
        {
            return physicalPeopleDal.GetListByOrderId(oid);
        }


        /// <summary>
        /// 根据订单id删除该订单信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int del_order(int id)
        {
            return orderDal.del_order(id);
        }

       

        /// <summary>
        /// 添加团体订单
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public long Add_tuanorder(int Sonnum, int Yusuan, string Uname, string Utell, string Tdate, int HospitalId, int Tstate)
        {
            var tuanDTO = new TuanOrderDTO();
            tuanDTO.Sonnum = Sonnum;
            tuanDTO.Yusuan = Yusuan;
            tuanDTO.Uname = Uname;
            tuanDTO.Utell = Utell;
            tuanDTO.Tdate = Tdate;
            tuanDTO.HospitalId = HospitalId;
            tuanDTO.Tstate = Tstate;
            return orderDal.Add_tuanorder(tuanDTO);
        }


         /// <summary>
        /// 根据id获取团检订单信息
        /// </summary>
        /// <returns></returns>
        public List<TuanOrderDTO> GettuanorderbyId(int id)
        {
            return orderDal.GettuanorderbyId(id);

        }

        /// <summary>
        /// 根据交易号更新订单状态
        /// 说明：用户支付成功回调
        /// </summary>
        /// <param name="payCode"></param>
        /// <returns></returns>
        public bool UpdateStateByPayCode(string payCode)
        {
            return orderDal.UpdateSate(payCode, (int)OrderState.ToAppointment);
        }

         /// <summary>
        /// 获取最新的订单编号
        /// </summary>
        /// <returns></returns>
        public string OrderString()
        {
            return orderDal.OrderString();
        }

          /// <summary>
        /// 写入订单详情表
        /// </summary>
        /// <param name="code"></param>
        /// <param name="userid"></param>
        /// <param name="codeid"></param>
        /// <param name="counts"></param>
        /// <returns></returns>
        public bool InsertOrderdetails(string code,int userid,int codeid,int counts)
        {
            return orderDal.InsertOrderdetails(code, userid, codeid, counts);
        }

    }
}
